package com.wyd.common.base;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.io.Serializable;
import java.util.Date;

@ToString
@Getter
@MappedSuperclass
public abstract class TimedEntity implements Serializable {
//    @CreatedDate
    @Column(updatable = false)
//    @CreationTimestamp
    @ApiModelProperty(value = "创建时间", accessMode = ApiModelProperty.AccessMode.READ_ONLY)
    private Date createdAt;

//    @LastModifiedDate
//    @UpdateTimestamp
    @ApiModelProperty(value = "最后修改时间", accessMode = ApiModelProperty.AccessMode.READ_ONLY)
    private Date updatedAt;

    @PrePersist
    protected void prePersist() {
        this.createdAt = new Date();
        this.updatedAt = this.createdAt;
    }

    @PreUpdate
    protected void preUpdate() {
        this.updatedAt = new Date();
    }
}
